package com.androidtv.pos.ui.fragment;

import android.os.Bundle;
import android.view.View;

import com.androidhelper.module.common.base.BaseFragment;
import com.androidhelper.module.common.bean.BaseResponse;
import com.androidhelper.module.common.http.control.BaseObserver;
import com.androidhelper.module.common.http.control.RxSchedulers;
import com.androidhelper.module.common.utils.IsEmptyUtils;
import com.androidtv.pos.R;
import com.androidtv.pos.adapter.RightMainFragmentPagerAdapter;
import com.androidtv.pos.http.repo.MineRepo;
import com.androidtv.pos.modelbean.ProjectClassifyData;
import com.androidtv.pos.ui.fragment.mealfragment.MealFragment;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import io.reactivex.Observable;

public class RightMainFragment2 extends BaseFragment {


    @BindView(R.id.slidingTabLayout)
    SlidingTabLayout slidingTabLayout;

    @BindView(R.id.viewPager)
    ViewPager viewPager;

  //  private final String[] mTitlesTabArr = {"热门", "iOS", "Android", "前端", "后端开发", "设计", "工具资源","数据库","云计算&大数据&人工智能","A","游戏","全栈工程师","运维"};

    List<ProjectClassifyData> mProjectClassifyDataList;
    private List<BaseFragment> mFragmentList;
    private List<String> mTitleList;


    public static RightMainFragment2 newInstance() {
        Bundle args = new Bundle();
        RightMainFragment2 fragment = new RightMainFragment2();
        fragment.setArguments(args);
        return fragment;
    }



    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_main_right2;
    }

    @Override
    protected void initView(View view, @Nullable Bundle savedInstanceState) {
        mProjectClassifyDataList = new ArrayList<>();//存储网络实体数据
          mTitleList = new ArrayList<>();
          mFragmentList = new ArrayList<>();
         /*for(int i=0;i<mTitlesTabArr.length;i++){
            mFragmentList.add(MealFragment.newInstance(i,mTitlesTabArr[i]));
        }*/

       //  viewPager.setAdapter( new RightMainFragmentPagerAdapter(getChildFragmentManager(),mFragmentList,mTitlesTabArr));
       //  slidingTabLayout.setViewPager(viewPager);




    //  viewPager.setOffscreenPageLimit(3);
        viewPager.setCurrentItem(0);
    }









    @Override
    protected void setListener() {
        slidingTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                slidingTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    @Override
    protected void initEventAndData(Bundle savedInstanceState) {
           //网络请求 tablayout 标题
        Observable<BaseResponse<List<ProjectClassifyData>>> projectClassifyData = MineRepo.getInstance().getService().getProjectClassifyData();
        projectClassifyData.compose(RxSchedulers.observableIO2Main())
                        .subscribe(new BaseObserver<List<ProjectClassifyData>>(){

                            @Override
                            public void onSuccess(List<ProjectClassifyData> projectClassifyDataList) {

                                showProjectClassifyData(projectClassifyDataList);

                            }

                            @Override
                            public void onFailure(Throwable e, String errorMsg) {

                            }
                        });
    }

    private void showProjectClassifyData(List<ProjectClassifyData> projectClassifyDataList) {
        mProjectClassifyDataList.clear();
        mProjectClassifyDataList.addAll(projectClassifyDataList);
         if(!IsEmptyUtils.isEmpty(mProjectClassifyDataList)&&mProjectClassifyDataList.size()>0){
             for (ProjectClassifyData projectClassifyData : mProjectClassifyDataList) {
                 mTitleList.add(projectClassifyData.getName());
                 mFragmentList.add(MealFragment.newInstance(projectClassifyData.getName(),projectClassifyData.getId()));
             }
         }

        viewPager.setAdapter( new RightMainFragmentPagerAdapter(getChildFragmentManager(),mTitleList,mFragmentList));
        slidingTabLayout.setViewPager(viewPager);


    }
}

